// router/router.go
package router

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	api "monitor_serve/api"
	common "monitor_serve/commons/jwt"
	"monitor_serve/service"
)

func Router() *gin.Engine {
	router := gin.Default()

	var (
		excelApi   = api.ApiGroupApp.ExcelDataApi
		fileSimple = api.ApiGroupApp.FileSimpleApi
		fileMeta   = api.ApiGroupApp.FileMetaApi
		userApi    = api.ApiGroupApp.UserApi
		muFileApi  = api.ApiGroupApp.MultipartFileApi
	)

	//解决跨域问题
	router.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"}, // 允许前端开发服务器地址
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
	}))
	publicGroup := router.Group("/public")
	{
		publicGroup.POST("/cu", service.CreateUser)
		publicGroup.POST("/login", service.JWTLogin)

		//jwt续期
		publicGroup.GET("/refresh_token", common.RefreshToken)

		//文件上传
		publicGroup.POST("/upload", fileSimple.UploadHandler)
		publicGroup.POST("/mpUploadInit", muFileApi.InitiateMultipartUploadHandler)
		//上传分块
		publicGroup.POST("/mpUploadApart", muFileApi.UploadMultipartHandler)
		//通知分块上传完成
		publicGroup.POST("/mpUploadComplete", muFileApi.CompleteUploadHandler)
		//分页查询
		publicGroup.GET("/queryFileByNamePage", fileSimple.QueryFileByUsernamePage)
		//文件下载
		publicGroup.GET("/downloadFile", fileSimple.DownloadHandler)
		//获取文件下载url
		publicGroup.GET("/downloadFileUrl", fileSimple.DownloadUrlHandler)
	}

	//这是给app用的
	api2 := router.Group("/api2")
	{
		//文件上传
		api2.POST("/upload", fileSimple.UploadHandler)
		//查询用户文件
		api2.GET("/queryFileByName", fileSimple.QueryFileByUsername)
		//获取文件元数据
		api2.GET("/getFileMeta", fileMeta.GetFileMetaHandler)
		//下载文件
		api2.GET("/downloadFile", fileSimple.DownloadHandler)
		//删除文件
		api2.DELETE("/deleteFile", fileSimple.DeleteHandler)
		//更新文件元数据
		api2.POST("/updateFileMeta", fileMeta.FileMetaUpdateHandler)
		//用户注册
		api2.POST("/userSignUp", userApi.UserSignUpApi)
		//用户登录
		api2.POST("/userSignIn", userApi.UserSignInApi)
		//上传excel文件接口
		api2.POST("/receiveExcelData", excelApi.ReceiveExcelDataApi)
		//获取用户信息
		api2.GET("/getUserInfo", userApi.GetUserInfoApi)
		//秒传
		api2.POST("/tryFastUpload", fileSimple.TryFastUploadHandler)
		//初始化分块信息
		api2.POST("/mpUploadInit", muFileApi.InitiateMultipartUploadHandler)
		//上传分块
		api2.POST("/mpUploadApart", muFileApi.UploadMultipartHandler)
		//通知分块上传完成
		api2.POST("/mpUploadComplete", muFileApi.CompleteUploadHandler)
		//取消上传分块
		//查看分块上传的整体状态
		//测试分块上传
		api2.POST("/mpUploadTest", muFileApi.StartMultipartUploadHandler)
		//分页查询
		api2.GET("/queryFileByNamePage", fileSimple.QueryFileByUsernamePage)
	}
	//这是给网页用的，这是有token的。
	api3 := router.Group("/api3")
	{
		api3.Use(common.JwtMiddleware())
		//上传文件
		api3.POST("/upload", fileSimple.UploadHandler)
		//获取文件元数据
		api3.GET("/getFileMeta", fileMeta.GetFileMetaHandler)
		//下载文件
		api3.GET("/downloadFile", fileSimple.DownloadHandler)
		//删除文件
		api3.DELETE("/deleteFile", fileSimple.DeleteHandler)
		//更新文件元数据
		api3.POST("/updateFileMeta", fileMeta.FileMetaUpdateHandler)
	}

	router.Static("/mo", "./resource/dist")
	return router
}
